<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    menu {
      width: 1200px;
      margin: 0 auto;
      padding: 0;
      /* border: 1px solid black; */
    }

    .m-search {
      width: 100%;
      border: 1px solid pink;
    }

    .m-table table {
      width: 100%;
      text-align: center;
      /* border: 1px blue; */
      border-collapse: collapse;
    }

    .m-table table td {
      height: 40px;
      border: 1px solid blue;
    }
  </style>
</head>

<body>
  <menu>
    <div class="m-search">
      姓名:<input type="text" id="name"><button>搜索</button><a href="add.html">添加</a>
    </div>
    <div class="m-table">
      <table>
        <thead>
          <tr>
            <th>序号</th>
            <th>学号</th>
            <th>姓名</th>
            <th>班级</th>
            <th>性别</th>
            <th>年龄</th>
            <th>电话</th>
            <th>爱好</th>
            <th>地址</th>
            <th>备注</th>
            <th>日期</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>

        </tbody>
      </table>
    </div>
  </menu>
  <script>
    var del
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
          var arr = JSON.parse(xhr.response)
          var str = ``
          arr.forEach(function (ele, i) {
            str += `
            <tr>
            <td>${i + 1}</td>
            <td>${ele.id}</td>
            <td>${ele.name}</td>
            <td>${ele.clazz}</td>
            <td>${ele.gender}</td>
            <td>${ele.age}</td>
            <td>${ele.tel}</td>
            <td>${ele.hobby}</td>
            <td>${ele.address}</td>
            <td>${ele.remark}</td>
            <td>${ele.date}</td>
            <td>
              <a href="javascript:void" onclick="to(${ele.id})">编辑</a>
              <a href="javascript:void" onclick="del(${ele.id})">删除</a>

            </td>
            </tr>
            `
          });
          document.querySelector('tbody').innerHTML = str
        }
      }
    }
    xhr.open("GET", ' http://localhost:3008/api/student/getstudent')
    xhr.send()


    var btn = document.querySelector('button')
    var inp = document.querySelector('input')
    btn.addEventListener('click', function (e) {
      var xhr = new XMLHttpRequest()
      xhr.onreadystatechange = function () {
        //判断请求状态
        if (xhr.readyState == 4) {
          if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
            var arr = JSON.parse(xhr.response)
            var str = ``
            arr.forEach(function (ele, index) {
              if (ele.name.search(inp.value) != -1) {
                str += `
                <tr>
                  <td>${index + 1}</td>
                  <td>${ele.id}</td>
                  <td>${ele.name}</td>
                  <td>${ele.clazz}</td>
                  <td>${ele.gender}</td>
                  <td>${ele.age}</td>
                  <td>${ele.tel}</td>
                  <td>${ele.hobby}</td>
                  <td>${ele.address}</td>
                  <td>${ele.remark}</td>
                  <td>${ele.date}</td>
                  <td>
                    <a href="javascript:void" onclick="to(${ele.id})">编辑</a>
                    <a href="javascript:void" onclick="del(${ele.id})">删除</a>


                  </td>
                  </tr>
                `
              }
            })
            document.querySelector('tbody').innerHTML = str
          }
        }
      }
      xhr.open("GET", ' http://localhost:3008/api/student/getstudent')
      xhr.send()
    })

    function del(id) {
      var flag = confirm('是否确定删除')
      if (flag) {
        var xhr = new XMLHttpRequest()
        xhr.onreadystatechange = function () {
          //判断请求状态
          if (xhr.readyState == 4) {
            if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
              var arr = JSON.parse(xhr.response)
              var str = ``
              arr.forEach(function (ele, index) {
                if (ele.name.search(inp.value) != -1) {
                  str += `
                <tr>
                  <td>${index + 1}</td>
                  <td>${ele.id}</td>
                  <td>${ele.name}</td>
                  <td>${ele.clazz}</td>
                  <td>${ele.gender}</td>
                  <td>${ele.age}</td>
                  <td>${ele.tel}</td>
                  <td>${ele.hobby}</td>
                  <td>${ele.address}</td>
                  <td>${ele.remark}</td>
                  <td>${ele.date}</td>
                  <td>
                    <a href="javascript:void" onclick="to(${ele.id})">编辑</a>
                    <a href="javascript:void" onclick="del(${ele.id})">删除</a>

                  </td>
                  </tr>
                `
                }
              })
              document.querySelector('tbody').innerHTML = str
            }
          }
        }
        xhr.open("POST", ' http://localhost:3008/api/student/removeStudent')
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send('id=' + id)
      }
    }
    function to(id) {
      window.location.href = "edit.html?id=" + id
    }
  </script>
</body>

</html>